﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using SD.Database.Classes;
using DBAccess;
using System.Collections.Generic;

namespace SD.Database
{
    public partial class StoredProcedures
    {
        public static void BuySu(int pId, int quantity)
        {
            int oldSuPrice = int.Parse(Configuration.GetConfigurationForOption("SupplyUnitsPrice"));
            int newPrice = oldSuPrice + quantity * 3;
            int buyPrice = oldSuPrice / 3 + 2 * newPrice / 3;
            int cash = (int)UserFunctions.GetPlayerFloat(pId, "Cash");
            if (cash >= quantity * buyPrice)
            {
                DAL.GetInstance().UpdateData("Update players SET SupplyUnits = SupplyUnits + @param0, Cash = Cash - @param1 Where PlayerId = @param2", new List<object>() { quantity, quantity * buyPrice, pId });
                DAL.GetInstance().UpdateData("Update Configuration SET SupplyUnitsPrice = @param0", new List<object>() { newPrice });
            }
        }

        public static void SellSu(int pId, int quantity)
        {
            int oldSuPrice = int.Parse(Configuration.GetConfigurationForOption("SupplyUnitsPrice"));
            int newPrice = oldSuPrice - quantity * 3;
            int sellPrice = oldSuPrice / 3 + 2 * newPrice / 3;
            int su = UserFunctions.GetPlayerInt(pId, "SupplyUnits");
            if (quantity <= su)
            {
                DAL.GetInstance().UpdateData("Update players SET SupplyUnits = SupplyUnits - @param0, Cash = Cash + @param1 Where PlayerId = @param2", new List<object>() { quantity, quantity * sellPrice, pId });
                DAL.GetInstance().UpdateData("Update Configuration SET SupplyUnitsPrice = @param0", new List<object>() { newPrice });
            }
        }
    };
}